package com.xinchuang.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.generator.SnowflakeGenerator;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xinchuang.comment.R;
import com.xinchuang.common.KeyWordSystem;
import com.xinchuang.common.Result;
import com.xinchuang.common.ResultUtil;
import com.xinchuang.constant.ImageTypeConstant;
import com.xinchuang.entity.common.ImgTargetTable;
import com.xinchuang.service.RedisUseService;
import com.xinchuang.service.UploadingService;
import com.xinchuang.service.common.ImgTargetTableService;
import com.xinchuang.service.image.GDALService;
import com.xinchuang.service.image.impl.GDALServiceImpl;
import com.xinchuang.util.FileElseUtil;
import com.xinchuang.utils.CheckUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.*;
import java.text.SimpleDateFormat;

import com.xinchuang.utils.DateUtils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.bind.annotation.*;
import com.alibaba.fastjson.JSON;

@Service
public class UploadingServiceImpl implements UploadingService {

    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    private SnowflakeGenerator snow = new SnowflakeGenerator(1, 1);

    @Autowired
    private ImgTargetTableService imgTargetTableService;

    @Autowired
    private GDALService gdalService;

    @Autowired
    private RedisUseService redisUseService;

    @Autowired
    private KeyWordSystem keyWordSystem;

    @Override
    public Result imgUpload(JSONObject jsonObject) {
        String tarId = jsonObject.getString("tarId");
        if (StringUtils.isNotBlank(redisUseService.getTypeName(tarId)) || StringUtils.isNotBlank(redisUseService.getSubTypeName(tarId))) {
            JSONArray position = jsonObject.getJSONArray("position");
            if (CollectionUtil.isEmpty(position)) {
                return ResultUtil.error(500, "请选择当前的经纬度");
            }
            JSONArray jsonArray = jsonObject.getJSONArray("imgList");
            //删除之前上传的影像
            imgTargetTableService.lambdaUpdate().eq(ImgTargetTable::getTarId, tarId).remove();
            if (CollectionUtil.isNotEmpty(jsonArray)) {
                List<ImgTargetTable> tables = new ArrayList<>();

                for (int i = 0; i < jsonArray.size(); i++) {
                    JSONObject img = jsonArray.getJSONObject(i);

                    String type = img.getString("typeName");
                    String mode = img.getString("typeValue");
                    String imgUrl = img.getString("fileUrl");
                    String thumb = img.getString("icon");
                    String imgName = img.getString("imgName");
                    String imgValue = img.getString("imgUrl");

                    ImgTargetTable imgTargetTable = new ImgTargetTable();
                    imgTargetTable.setTarId(tarId);
                    imgTargetTable.setType(type);
                    imgTargetTable.setMode(mode);
                    imgTargetTable.setImgUrl(imgUrl);
                    imgTargetTable.setThumbnail(thumb);
                    imgTargetTable.setName(imgName);
                    imgTargetTable.setImgValue(imgValue);

                    JSONArray lngLat = GDALServiceImpl.getTifCenterLngLat(
                            keyWordSystem.getFILE_URL() + imgUrl,
                            imgTargetTable,
                            true,
                            position.getDouble(0),
                            position.getDouble(1)
                    );
                    imgTargetTable.setImgPosition(lngLat.toJSONString());

                    tables.add(imgTargetTable);
                }
                imgTargetTableService.saveBatch(tables);
            }
            return ResultUtil.success();
        } else {
            return ResultUtil.error(500, "请选择类型");
        }
    }

    @Override
    public Result getImgList(String typeId) {
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("tarId", typeId);
        resultMap.put("position", ListUtil.empty());
        resultMap.put("imgList", ListUtil.empty());
        List<ImgTargetTable> imgTargetTables = imgTargetTableService.lambdaQuery().eq(ImgTargetTable::getTarId, typeId).list();
        if (CollectionUtil.isNotEmpty(imgTargetTables)) {
            List<JSONObject> jsonObjects = new ArrayList<>();
            for (ImgTargetTable imgTargetTable : imgTargetTables) {
                JSONObject img = new JSONObject();
                img.put("typeName", imgTargetTable.getType());
                img.put("typeValue", imgTargetTable.getMode());
                img.put("fileUrl", imgTargetTable.getImgUrl());
                img.put("url", imgTargetTable.getImgUrl());
                img.put("icon", imgTargetTable.getThumbnail());
                img.put("imgName", imgTargetTable.getName());
                img.put("imgUrl", imgTargetTable.getImgValue());
                jsonObjects.add(img);
                resultMap.put("position", FileElseUtil.getObjectToList(imgTargetTable.getPosition()));
            }
            resultMap.put("imgList", jsonObjects);
        }
        return ResultUtil.success(resultMap);
    }

}
